﻿/*
 * The MIT License (MIT)
 * Copyright (c) 2012 @donkeysharp
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software 
 * is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies 
 * or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DonkeyCleaner.Core {
    /// <summary>
    /// Enum that represents scanning method
    /// </summary>
    public enum ScanningType {
        DirectoryClonScan,
        FileClonScan,
        DatabaseScan,
        CompleteScan
    }
    public class MainCleaner {
        /// <summary>
        /// General method for calling in a simple way
        /// all scanning methods
        /// </summary>
        /// <param name="rootPath">Root folder to start scanning</param>
        /// <param name="scanningType">Scanning method</param>
        /// <returns>List of malware</returns>
        public List<Malware> GetMalwareList(string rootPath, ScanningType scanningType) {
            MalwareScanner scanner = new MalwareScanner(rootPath);

            // HACK: Awful but I just figured out this solution, 
            // I know it's the worst one, but I just had no time to refactor it
            List<Malware> completeScanList;
            List<Malware> directoryScanList;
            List<Malware> fileScanList;
            List<Malware> databaseScanList;

            directoryScanList = scanner.DirectoryRecursiveMalwareScan();
            fileScanList = scanner.FileRecursiveMalwareScan();
            databaseScanList = scanner.HashBasedRecursiveScan();

            // Returns list of malware according to scanningType
            switch (scanningType) {
                case ScanningType.DirectoryClonScan:
                    return directoryScanList;
                case ScanningType.FileClonScan:
                    return fileScanList;
                case ScanningType.DatabaseScan:
                    return databaseScanList;
            }
            completeScanList = new List<Malware>();
            completeScanList.AddRange(directoryScanList);
            completeScanList.AddRange(fileScanList);
            completeScanList.AddRange(databaseScanList);

            return completeScanList;
        }
        /// <summary>
        /// Deletes all files in malware list
        /// </summary>
        /// <param name="malwareList">File to be deleted</param>
        public void DeleteMalware(List<Malware> malwareList) {
            foreach(var malware in malwareList) {
                try {
                    File.Delete(malware.Name);
                }
                catch (Exception ex) {
                    Logging.Logger.SaveLogMessage(Logging.LogType.Other, "Error deleting file: " + malware.Name + " - Exception: " + ex.Message);
                }
            }
        }
        /// <summary>
        /// Updates malware database
        /// </summary>
        /// <returns>True if database updated</returns>
        public bool UpdataMalwareDatabase() {
            Updater updater = new Updater();
            return updater.UpdateDatabase();
        }
    }
}
